//
//  PolicyController.m
//  Village
//
//  Created by 程虎 on 2017/9/1.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "PolicyController.h"
#import "UITableViewCell+FSAutoCountHeight.h"
#import "PolicyDetailViewController.h"
#import "PolicyTableViewCell.h"
#import "TPolicyTableViewCell.h"
#import "SearchView.h"
#import <MJRefresh.h>
#import "FSEntity.h"
#import "PolicyModel.h"
#import "PolicyHttpTools.h"
#import "ShowEmptyView.h"
@interface PolicyController ()<UITableViewDelegate,UITableViewDataSource>
{
    int inta;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *tableView1;
@property (nonatomic, assign) BOOL isDebug;
@property (nonatomic, strong) FSEntity *entity;
@property (nonatomic, strong) PolicyModel *policy;
@property (nonatomic ,strong) NSMutableArray *dataAry;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageCount;//总页数 至少一页 初始值为2
@end

@implementation PolicyController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
//    self.dataAry = [NSMutableArray arrayWithArray:[PolicyModel mj_objectArrayWithKeyValuesArray:[Globle getInstance].policyArys]];
    [self getdata:1];
    if ([Globle getInstance].account.userid.length > 0){
        if ([Globle getInstance].isNormal){
            [self setupSubViews1];
        }else{
            [self setupSubViews];
        }
    }else{
        [self setupSubViews1];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.pageNum = 0;
    self.pageCount = 0;
}
- (void)setupSubViews1{
    _tableView.hidden = YES;
    ShowEmptyView *show = [[ShowEmptyView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight -64)];
    [self.view addSubview:show];
}
- (void)setupSubViews
{
//    SearchView *searchs = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 64) Type:1 ShowView:self];
//    searchs.placeholderStr = @"搜索村落";
//    searchs.locationStr = [Globle getInstance].locations.City;
//    searchs.searchBlock = ^(NSString *searchStr) {
//        LSLog(@"search ------  %@",searchStr);
//    };
//    [self.view addSubview:searchs];
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//   _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//去除多余空白行
    [_tableView registerClass:[PolicyTableViewCell class] forCellReuseIdentifier:@"PolicyTableViewCell"];
    [_tableView registerClass:[TPolicyTableViewCell class] forCellReuseIdentifier:@"TPolicyTableViewCell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(0);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshheader_data)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshfoot_data)];
}

#pragma mark - 下拉刷新
- (void)refreshheader_data
{
    self.pageNum = 1;
    self.pageCount = 0;
    [self.tableView.mj_footer setState:MJRefreshStateIdle];
    [self getdata:self.pageNum];
}

#pragma mark - 上拉刷新
- (void)refreshfoot_data
{
    self.pageNum += 1;
    if (self.pageNum < self.pageCount + 1) {
        [self getdata:self.pageNum];
    }
    else {
        [EmptyFunction hideEmpty:self.tableView];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}

#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PolicyModel *policy = _dataAry[indexPath.row];
    CGFloat height = [self.title isEqualToString:@"keyCache"]?[TPolicyTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cacheKey:policy.title cellContentViewWidth:0 bottomOffset:0]:[TPolicyTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:0];
    return height+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell *cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:@"TPolicyTableViewCell"];
        TPolicyTableViewCell *newcell = (TPolicyTableViewCell *)cell;
        newcell.isDebug = self.isDebug;
        newcell.policy = _dataAry[indexPath.row];
        [newcell setSelectionStyle:UITableViewCellSelectionStyleNone];//cell点击之后没有视觉效果
    return cell;
}

- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{

    [self addReadNum:_dataAry[indexPath.row]];
}
#pragma mark - 加载数据
- (void)getdata:(NSInteger )index
{
    WeakSelf(weakSelf);
    [PolicyHttpTools GetPolicyWithPageNum:index Success:^(NSDictionary *valueDict) {
        if (index == 1) {
            [weakSelf.tableView.mj_header endRefreshing];
            self.pageCount = [valueDict[@"pagecount"] integerValue];
            self.dataAry = [PolicyModel mj_objectArrayWithKeyValuesArray:valueDict[@"list"]];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshing];
            NSArray *array = self.dataAry;
            self.dataAry = [NSMutableArray arrayWithArray:array];
            [self.dataAry addObjectsFromArray:[PolicyModel mj_objectArrayWithKeyValuesArray:valueDict[@"list"]]];
        }
//        [[Globle getInstance] savePolicy:[PolicyModel mj_keyValuesArrayWithObjectArray:self.dataAry]];
        [EmptyFunction hideEmpty:self.tableView];
        [self.tableView reloadData];
    } failure:^(NSString *error) {
        if (index == 1) {
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [EmptyFunction showEmpty:self.tableView Height:(KDeviceHeight - kNavHeight -49)];
    }];
}
#pragma mark - 增加阅读数
- (void)addReadNum:(PolicyModel *)model
{
    WeakSelf(weakSelf);
    [Tools ToNextName:@"PolicyDetailViewController" Params:model CurrentVC:weakSelf Completion:^(id obj) {
    }];
    [PolicyHttpTools AddPolicyReadNumWithID:model.ID Success:^{
    } failure:^(NSString *error) {
    }];
}

@end
